#ifndef HOSTWIDGET_H
#define HOSTWIDGET_H

#include <QWidget>
#include <QVBoxLayout>
#include <QLabel>
#include <QComboBox>
#include <QLineEdit>
#include <QPushButton>
#include <QCheckBox>
#include <QtNetwork>
#include <QMessageBox>

#include "PortMappingThread.h"
#include "Network.h"
#include "GuiParameters.h"

class HostWidget : public QWidget {

    Q_OBJECT

    typedef enum AddressComboBoxIndices {
        PUBLIC_BOX = 0,
        LOCAL_BOX,
        LOCALHOST_BOX,
        OTHER_BOX
    } AddressComboBoxIndices;

    typedef enum PortError {
        ADDRESS_ERROR,
        ROUTER_ERROR
    } PortError;

public:
    explicit HostWidget(QWidget *parent = 0, Network::ConnectionStatus * status = 0);
    ~HostWidget();

    QTcpSocket * clientSocket;

signals:
    void connectionStatusChanged(Network::ConnectionStatus status);

public slots:
    void changeConnectionStatus(Network::ConnectionStatus status);

private slots:
    void hostOnClick();
    void addressActivated(int index);
    void portCheckStateChanged(int state);
    void assignPublicIP(QNetworkReply * reply);
    void serverNewConnection();
    void socketDisconnect();
    void connectionError(QAbstractSocket::SocketError socketError);
    void changePortStatus(int status);

private:
    void createWidgets();
    void createLayouts();
    void connectSignalSlots();
    void getPublicIP();
    void getLocalIP();
    void portErrorMessage(PortError error);

    QNetworkAccessManager * accessManager;
    QTcpServer * serverSocket;
    Network::ConnectionStatus * connectionStatus;

    QString publicIPString;
    QString localIPString;

    QLabel * addressLabel;
    QComboBox * addressComboBox;
    QLineEdit * addressEdit;
    QPushButton * hostButton;
    QCheckBox * portCheckBox;

    Network::PortStatus portStatus;
    PortMappingThread * portMappingThread;
};

#endif // HOSTWIDGET_H
